// detail.js
var that;

Page({
    onLoad: function (options) {
        that = this;
        that.loadOrder(options.index);
        getApp().loadSeller(function (seller) {
            that.setData({
                seller: seller
            });
        });
    },
    loadOrder: function (index) {
        // 从本地存储中获取订单详情
        var allOrders = wx.getStorageSync('orderList') || [];
        var order = allOrders[index];
        
        if (order) {
            that.setData({
                order: order
            });
        } else {
            wx.showToast({
                title: '订单不存在',
                icon: 'none'
            });
        }
    },
    contact: function () {
        var telephone = that.data.seller.telephone;
        wx.makePhoneCall({
            phoneNumber: telephone //仅为示例，并非真实的电话号码
        })
    },
    payment: function () {
        // 支付
        getApp().payment(that.data.order);
    },
    cancel: function () {
        // 取消确认
        wx.showModal({
            title: '确定要取消订单吗？',
            success: function (res) {
                if (res.confirm) {
                    // 取消订单
                    var order = that.data.order;
                    order.status = -1;
                    
                    // 更新本地存储中的订单状态
                    var allOrders = wx.getStorageSync('orderList') || [];
                    var index = allOrders.findIndex(o => o.id === order.id);
                    if (index !== -1) {
                        allOrders[index] = order;
                        wx.setStorageSync('orderList', allOrders);
                    }

                    wx.showToast({
                        title: '订单已取消',
                        success: function () {
                            that.setData({
                                order: order
                            });
                        }
                    });
                }
            }
        });
    },
    callReceiver: function (e) {
        var telephone = e.currentTarget.dataset.telephone;
        wx.makePhoneCall({
            phoneNumber: telephone //仅为示例，并非真实的电话号码
        })
    }
})
